<template>
  <div class="articles">
    <van-cell v-for="(item, i) in list" :key="i">
      <div class="cell-header">
        <van-image
          width="30"
          height="30"
          :src="
            item.original_status.user.photo_domain +
            item.original_status.user.profile_image_url.split(',')[0]
          "
        />
        <div class="right-header">
          <div class="author">{{ item.original_status.user.screen_name }}</div>
          <div class="pubtime">{{ item.original_status.timeBefore }}</div>
        </div>
      </div>
      <p v-html="item.original_status.description"></p>
      <div class="cell-footer">
        <div class="item">
          <van-icon name="share-o"></van-icon>
          <div class="num">{{ item.original_status.retweet_count }}</div>
          <van-icon name="chat-o"></van-icon>
          <div class="num">{{ item.original_status.reply_count }}</div>
          <van-icon name="good-job-o"></van-icon>
          <div class="num">{{ item.original_status.like_count }}</div>
        </div>
      </div>
    </van-cell>

    <div class="finish" v-if="finish">已加载完毕</div>
  </div>
</template>

<script>
import { getArticles } from "@/api/index.js";
import { Cell, Image as VanImage, Icon } from "vant";
import debounce from "@/utils/debounce.js";

export default {
  name: "Articles.vue",
  data() {
    return {
      page: 1,
      list: [],
      finish: false,
    };
  },
  methods: {
    async getList() {
      let result = await getArticles({ page: this.page });
      if (result.result && result.result.indexOf("已加载") !== -1) {
        this.finish = true;
      } else {
        this.list = this.list.concat(result.items);
        this.page++;
      }
    },
  },
  async mounted() {
    this.getList();
    window.addEventListener(
      "scroll",
      debounce(
        () => {
          let scrollHeight = document.documentElement.scrollHeight;
          let scrollTop = document.documentElement.scrollTop;
          let innerHeight = window.innerHeight;
          if (scrollHeight - scrollTop - innerHeight < 50) {
            this.getList();
          }
        },
        200,
        false
      )
    );
  },
  components: {
    "van-cell": Cell,
    "van-image": VanImage,
    "van-icon": Icon,
  },
};
</script>

<style lang="less" scoped>
.cell-header {
  display: flex;
  height: 1rem;
  .right-header {
    font-size: 0.3rem;
    line-height: 0.4rem;
    padding-left: 0.2rem;
    .pubtime {
      color: #999;
    }
  }
}

.cell-footer {
  display: flex;
  height: 0.5rem;
  align-items: center;
  .item {
    display: flex;
    font-size: 0.3rem;
    width: 1.5rem;
    height: 0.5rem;
    align-items: center;
    .num {
      padding: 0 0.1rem;
    }
  }
}
</style>
